package com.newtcdemo.CustomViews.todayTopThreeChart;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.newtcdemo.R;
import com.newtcdemo.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/4/13.
 * 今日前三折线图
 */
public class TodayTopThreeChart extends LinearLayout {

    private RelativeLayout layoutBrokenLine;  //存放折线
    private TextView textBrokenLineNodeTips;
    private TodayTopThreeBrokenLine mBrokenLine;
    private float mMaxValue = 0;//取大值
    private List<Float> mMilliliter = new ArrayList<>();//存储各个竖线的高度
    private float mValue1 = 0, mValue2 = 0, mValue3 = 0, mValue4 = 0, mValue5 = 0, mValue6 = 0, mValue7 = 0;
    private TextView textOne,textTwo,textThree,textFour,textFive,textSix,textSeven,textTypeTitle;
    private Context mContext;

    public TodayTopThreeChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        LayoutInflater.from(context).inflate(R.layout.today_top_three, this, true);
        layoutBrokenLine = (RelativeLayout) findViewById(R.id.layoutBrokenLine);
        textBrokenLineNodeTips = (TextView) findViewById(R.id.textBrokenLineNodeTips);
        textTypeTitle=(TextView)findViewById(R.id.textTypeTitle);
        textOne=(TextView)findViewById(R.id.textOne);
        textTwo=(TextView)findViewById(R.id.textTwo);
        textThree=(TextView)findViewById(R.id.textThree);
        textFour=(TextView)findViewById(R.id.textFour);
        textFive=(TextView)findViewById(R.id.textFive);
        textSix=(TextView)findViewById(R.id.textSix);
        textSeven=(TextView)findViewById(R.id.textSeven);
        mBrokenLine = new TodayTopThreeBrokenLine(context);
        layoutBrokenLine.addView(mBrokenLine);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        moveTips(mBrokenLine.getShowIndex());
        return false;
    }

    //移动点击提示域
    private void moveTips(int i){
        if (layoutBrokenLine.getWidth() > 0) {
            int bklNodeTipsMAXWidth=layoutBrokenLine.getWidth()/7- DisplayUtil.dip2px(mContext,6);
            int marginLeftWidth=layoutBrokenLine.getWidth()/7*i;
            FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(bklNodeTipsMAXWidth,ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(marginLeftWidth + DisplayUtil.dip2px(mContext,6)/2,0,0,0);
            textBrokenLineNodeTips.setLayoutParams(layoutParams);
            if (mMilliliter.size()>0){
                textBrokenLineNodeTips.setText(String.valueOf(mMilliliter.get(i).intValue()));
            }
        }
    }

    //设置标题
    public void setTypeTitle(String title){
        textTypeTitle.setText(title);
    }

    //设置各个进度
    public TodayTopThreeChart setProgressValue(float value1, float value2, float value3, float value4, float value5, float value6, float value7) {
        mMaxValue = 0;//
        this.mValue1 = value1 < 0 ? 0 : value1;
        if (value1 > mMaxValue) mMaxValue = value1;
        this.mValue2 = value2 < 0 ? 0 : value2;
        if (value2 > mMaxValue) mMaxValue = value2;
        this.mValue3 = value3 < 0 ? 0 : value3;
        if (value3 > mMaxValue) mMaxValue = value3;
        this.mValue4 = value4 < 0 ? 0 : value4;
        if (value4 > mMaxValue) mMaxValue = value4;
        this.mValue5 = value5 < 0 ? 0 : value5;
        if (value5 > mMaxValue) mMaxValue = value5;
        this.mValue6 = value6 < 0 ? 0 : value6;
        if (value6 > mMaxValue) mMaxValue = value6;
        this.mValue7 = value7 < 0 ? 0 : value7;
        if (value7 > mMaxValue) mMaxValue = value7;
        return this;
    }

    //设置日期
    public void setDateValue(String value1, String value2, String value3, String value4, String value5, String value6, String value7) {
        textOne.setText(value1);
        textTwo.setText(value2);
        textThree.setText(value3);
        textFour.setText(value4);
        textFive.setText(value5);
        textSix.setText(value6);
        textSeven.setText(value7);
    }

    //刷新
    public void refreshView() {
        if (layoutBrokenLine.getWidth() > 0) {
            mMilliliter.clear();
            mMilliliter.add(mValue1);
            mMilliliter.add(mValue2);
            mMilliliter.add(mValue3);
            mMilliliter.add(mValue4);
            mMilliliter.add(mValue5);
            mMilliliter.add(mValue6);
            mMilliliter.add(mValue7);
            mBrokenLine.init(layoutBrokenLine.getWidth(), mMilliliter.size(), mMaxValue);
            mBrokenLine.setList(mMilliliter);
            textBrokenLineNodeTips.setVisibility(VISIBLE);
            mBrokenLine.setShowIndex(6);
            moveTips(6);
        }else {
            Log.e("TodayTopThreeChart","layoutBrokenLine.getWidth() is 0");
        }
    }

}